import java.awt.Container;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import javax.swing.JTextField;
import javax.swing.JToolBar;

public class Tape extends JToolBar
  {
  private Machine machine;
  private TapeTextField left;
  private TapeTextField right;
  private Step step;
  public Tape(Machine machine)
    {
    this.machine=machine;
    left=new TapeTextField(machine);
    right=new TapeTextField(machine);
    step=new Step(machine);

    left.setHorizontalAlignment(JTextField.RIGHT);
    right.setHorizontalAlignment(JTextField.LEFT);
    
    Container c=new Container();
    c.setLayout(new GridLayout(1,2));
    c.add(left);
    c.add(right);
    setLayout(new BorderLayout());
    add(c,"Center");
    add(step,"East");
    }
  public String getSymbol()
    {
    if(getRight().length()==0) return " ";
    else return getRight().substring(0,1);
    }
  public void step(String s,int m)
    {
    String l=getLeft();
    String r=getRight();
    if(r.length()==0)
      {
      r=" ";
      }
    r=s+r.substring(1);
    if(m>0)
      {
      r=r+getSpaces(m-r.length());
      l=l+r.substring(0,m);
      r=r.substring(m);
      }
    else if(m<0)
      {
      l=getSpaces(-m-l.length())+l;
      r=l.substring(l.length()-2-m)+r;
      l=l.substring(0,l.length()-2-m);
      }
    right.setText(r);
    left.setText(l);
    }
  private String getSpaces(int n)
    {
    String s="";
    int i=0;
    for(i=0;i<n;i++)
      {
      s=s+" ";
      }
    return s;
    }
  public String getLeft()
    {
    return left.getText();
    }
  public String getRight()
    {
    return right.getText();
    }
  public void setLeft(String s)
    {
    left.setText(s);
    }
  public void setRight(String s)
    {
    right.setText(s);
    }
  public void setEnabled(boolean set)
    {
    step.setEnabled(set);
    }
  }
